package br.com.rafsoft.mapaentrega;

import java.util.regex.Pattern;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import br.com.rafsoft.mapaentrega.adapter.MessageAdapter;
import br.com.rafsoft.mapaentrega.adapter.TaskAdapter;

public class MapaEntregaApplication extends Application {

	interface TaskListener {
		void onTaskUpdated(String message, long id);
	}

	private TaskListener listener;
	private TaskAdapter taskAdapter;
	private MessageAdapter messageAdapter;
	private String user = "rafael";
	private String userName = "Rafael Daniel Laurindo";

	public void setTaskListener(TaskListener listener) {
		this.listener = listener;
	}

	public TaskAdapter getTaskAdapter(Context context) {
		if (taskAdapter == null) {
			taskAdapter = new TaskAdapter(context);
		}

		return taskAdapter;
	}

	public void notifyListener(Intent intent) {
		if (listener != null) {
			Bundle extras = intent.getExtras();
			if (extras != null) {
				String message = (String) extras.get("message");
				String[] messages = message.split(Pattern.quote("|"));
				listener.onTaskUpdated(messages[0], Long.parseLong(messages[1]));
			}
		}
	}
	
	public MessageAdapter getMessageAdapter(Context context) {
		if (messageAdapter == null) {
			messageAdapter = new MessageAdapter(context);
		}

		return messageAdapter;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

}
